package hbase.crud;

import hbase.crud.entity.Student;
import hbase.crud.entity.StudentInfo;
import hbase.crud.entity.StudentScore;
import hbase.crud.util.HbaseClient;

import java.util.List;

public class StudentCRUDMain {

    public static void main(String[] args) {
        System.out.println("hello,let's go");
        HbaseClient client = new HbaseClient("jikehadoop02",2181);
        System.out.println("let's start");
        //删除旧表
        System.out.println("准备删除旧表和namesapce");
        client.dropStudentTableIfExists();
        //建表
        System.out.println("准备创建新namespace和新表");
        client.createStudentTable();
        //插入数据
        Student tom = new Student("Tom",new StudentInfo("20210000000001",1),new StudentScore(75,82));
        Student jerry = new Student("Jerry",new StudentInfo("20210000000002",1),new StudentScore(85,67));
        Student jack = new Student("Jack",new StudentInfo("20210000000003",2),new StudentScore(80,80));
        Student rose = new Student("Rose",new StudentInfo("20210000000004",2),new StudentScore(60,61));
        Student me = new Student("lh1",new StudentInfo("G20210735010189",2),new StudentScore(100,100));
        System.out.println("保存学生tom");
        client.saveStudent(tom);
        System.out.println("保存学生jerry");
        client.saveStudent(jerry);
        System.out.println("保存学生jack");
        client.saveStudent(jack);
        System.out.println("保存学生role");
        client.saveStudent(rose);
        System.out.println("保存学生lh1");
        client.saveStudent(me);
        System.out.println("保存所有学生完毕");
        //查询大家
        List<Student> list = client.scanStudent(5);
        System.out.println("查询学生数量:"+(list == null ? 0 :list.size()));
        if(list!=null){
            for(Student s:list){
                System.out.println(s);
            }
        }else{
            System.err.println("查询学生列表失败！!!");
        }
        //修改自己
        me.getScore().setUnderstanding(80);
        System.out.println("开始修改lh1的understanding的score为80");
        client.saveStudent(me);
        //查询自己
        Student newMe = client.getStudent("lh1");
        System.out.println("再次查询lh1的个人信息："+ newMe);

        //删除tom
        System.out.println("准备删除Tom的信息");
        client.deleteStudent("Tom");
        Student tomNew = client.getStudent("Tom");
        System.out.println("删除后再查找Tom结果："+tomNew);

        client.closeClient();
        System.out.println("byebye");
    }
}
